<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="签约编号" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入签约编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户编号" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付方式:数据字典" prop="payment">
        <el-select v-model="queryParams.payment" placeholder="请选择支付方式:数据字典" clearable>
          <el-option
            v-for="dict in pay_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['contractlog:contractlog:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['contractlog:contractlog:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['contractlog:contractlog:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['contractlog:contractlog:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="contractlogList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="签约编号" align="center" prop="id" />
      <el-table-column label="签约人姓名" align="center" prop="user.name" />
      <el-table-column label="身份证号" align="center" prop="user.idcard" />
      <el-table-column label="手机号码" align="center" prop="user.tel" />
      <el-table-column label="签约医生团队" align="center" prop="teamName" />
      <el-table-column label="签约医生" align="center" prop="doctorName" />
      <el-table-column label="服务包" align="center" prop="servicePackageName" />
      <el-table-column label="签约状态：已生效0，未生效1" align="center" prop="effectiveStatus" />
      <el-table-column label="到期时间" align="center" prop="expirationTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.expirationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Document" @click="handleDetail"
            v-hasPermi="['contractlog:contractlog:detail']">详情</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['contractlog:contractlog:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改签约管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="contractlogRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户编号" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户编号" />
        </el-form-item>
        <el-form-item label="签约周期" prop="cycle">
          <el-input v-model="form.cycle" placeholder="请输入签约周期" />
        </el-form-item>
        <el-form-item label="申请时间" prop="applicationTime">
          <el-date-picker clearable
            v-model="form.applicationTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择申请时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="生效日期" prop="effectiveDate">
          <el-date-picker clearable
            v-model="form.effectiveDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择生效日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="到期时间" prop="expirationTime">
          <el-date-picker clearable
            v-model="form.expirationTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择到期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="支付时间" prop="paytime">
          <el-date-picker clearable
            v-model="form.paytime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择支付时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="支付方式:数据字典" prop="payment">
          <el-select v-model="form.payment" placeholder="请选择支付方式:数据字典">
            <el-option
              v-for="dict in pay_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="签约备注">
          <editor v-model="form.remark" :min-height="192"/>
        </el-form-item>
        <el-form-item label="审批人" prop="reviewer">
          <el-input v-model="form.reviewer" placeholder="请输入审批人" />
        </el-form-item>
        <el-form-item label="审批时间" prop="approvalTime">
          <el-date-picker clearable
            v-model="form.approvalTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择审批时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="驳回原因" prop="reason">
          <el-input v-model="form.reason" placeholder="请输入驳回原因" />
        </el-form-item>
        <el-form-item label="最后修改时间" prop="lastModifyTime">
          <el-date-picker clearable
            v-model="form.lastModifyTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择最后修改时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Contractlog">
import { listContractlog, getContractlog, delContractlog, addContractlog, updateContractlog } from "@/api/contractlog/contractlog";

const { proxy } = getCurrentInstance();
const { pay_type } = proxy.useDict('pay_type');

const contractlogList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    id: null,
    userId: null,
    effectiveStatus: null,
    payment: null,
  },
  rules: {
    userId: [
      { required: true, message: "用户编号不能为空", trigger: "blur" }
    ],
    organization: [
      { required: true, message: "签约机构编号不能为空", trigger: "change" }
    ],
    team: [
      { required: true, message: "签约团队编号不能为空", trigger: "change" }
    ],
    doctor: [
      { required: true, message: "签约医生编号不能为空", trigger: "change" }
    ],
    servicePackage: [
      { required: true, message: "签约服务包编号不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询签约管理列表 */
function getList() {
  loading.value = true;
  listContractlog(queryParams.value).then(response => {
    contractlogList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    userId: null,
    status: null,
    effectiveStatus: null,
    organization: null,
    team: null,
    doctor: null,
    servicePackage: null,
    cycle: null,
    type: null,
    applicationTime: null,
    effectiveDate: null,
    expirationTime: null,
    paytime: null,
    payment: null,
    remark: null,
    reviewer: null,
    approvalTime: null,
    reason: null,
    lastModifyTime: null
  };
  proxy.resetForm("contractlogRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加签约管理";
}
const router = useRouter();
const handleDetail = () => {
  router.push({ path: '/contractinfo' });
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getContractlog(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改签约管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["contractlogRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateContractlog(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addContractlog(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除签约管理编号为"' + _ids + '"的数据项？').then(function() {
    return delContractlog(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('contractlog/contractlog/export', {
    ...queryParams.value
  }, `contractlog_${new Date().getTime()}.xlsx`)
}

getList();
</script>
